/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.tabel.views;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import org.opu.db_vdumper.ui.component.tabel.models.Pike;

/**
 *
 * @author Potochnak Yana
 */
public class LinePikeView extends PikeView {
    private Pike model;
    private final Point p1, p2;

    public LinePikeView(Pike model, int direction) {
        this.model = model;
        this.direction = direction;
        p1 = calc(direction + model.getTiltAngle());
        p2 = calc(direction - model.getTiltAngle());
    }

    public Pike getModel() {
        return model;
    }

    public void setModel(Pike model) {
        this.model = model;
    }

    @Override
    public void draw(Graphics2D g2, Point p) {
        g2.drawLine(p.x, p.y, p.x + p1.x, p.y + p1.y);
        g2.drawLine(p.x, p.y, p.x + p2.x, p.y + p2.y);

        Image toDraw = getImageBorder();
        //Рисуем обрамление (елсли оно есть)
        if (toDraw != null) {
            Dimension s = getPreferredSize();
            //g2.drawImage(toDraw, 1, 1, s.width - 1, s.height - 1, null);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        double l = model.getTiltAngle();
        int m = model.getLength();
        int l1 = (int) Math.round(m + Math.sin(Math.toRadians(l)));
        int l2 = l1;
        if (direction == PikeView.TOP_DIRECTION || direction == PikeView.BELOW_DIRECTION){
            l1 *= 2;
        } else {
            l2 *= 2;
        }
        return new Dimension(l1 + 1, l2 + 1);
    }

    @Override
    public Point[] points() {
        return new Point[]{new Point(p1), new Point(p2)};
    }


    public final Point calc(double gAngel){
        int row = 360;
        if (gAngel > row){
            gAngel = gAngel % row;
        } else if (gAngel < 0){
            gAngel = row + (gAngel % row);
        }

        double angle = Math.toRadians(gAngel);

        int x = (int) Math.round(model.getLength() / Math.sin(angle));
        int y = (int) Math.round(model.getLength() / Math.cos(angle));

        return new Point(y, x);
    }

}
